package com.wolfsea.binderdemo
import android.os.IBinder

/**
 *@desc CalculateNumberProxy
 *@author liuliheng
 *@time 2022/10/23  17:41
 **/
class CalculateNumberProxy(remote: IBinder) : ICalculateNumber {

    private val mRemote = remote

    override fun doSum(a: Int, b: Int): Int {
        val _data = android.os.Parcel.obtain()
        val _reply = android.os.Parcel.obtain()
        val _result: Int
        try {
            _data.writeInterfaceToken(CalculateNumberStub.Companion.DESCRIPTOR)
            _data.writeInt(a)
            _data.writeInt(b)
            val _status = mRemote.transact(CalculateNumberStub.Companion.TRANSACTION_doSum, _data, _reply, 0)
            if (!_status && CalculateNumberStub.getDefaultImpl() != null) {
                return CalculateNumberStub.getDefaultImpl()!!.doSum(a, b)
            }
            _reply.readException()
            _result = _reply.readInt()
        } finally {
            _reply.recycle()
            _data.recycle()
        }
        return _result
    }

    override fun doMinus(a: Int, b: Int): Int {
        val _data = android.os.Parcel.obtain()
        val _reply = android.os.Parcel.obtain()
        val _result: Int
        try {
            _data.writeInterfaceToken(CalculateNumberStub.Companion.DESCRIPTOR)
            _data.writeInt(a)
            _data.writeInt(b)
            val _status = mRemote.transact(CalculateNumberStub.Companion.TRANSACTION_doMinus, _data, _reply, 0)
            if (!_status && CalculateNumberStub.getDefaultImpl() != null) {
                return CalculateNumberStub.getDefaultImpl()!!.doSum(a, b)
            }
            _reply.readException()
            _result = _reply.readInt()
        } finally {
            _reply.recycle()
            _data.recycle()
        }
        return _result
    }

    override fun getPid(): Int {
        val _data = android.os.Parcel.obtain()
        val _reply = android.os.Parcel.obtain()
        val _result: Int
        try {
            _data.writeInterfaceToken(CalculateNumberStub.Companion.DESCRIPTOR)
            //_data.writeInt(a)
            //_data.writeInt(b)
            val _status = mRemote.transact(CalculateNumberStub.Companion.TRANSACTION_getPid, _data, _reply, 0)
            if (!_status && CalculateNumberStub.getDefaultImpl() != null) {
                return CalculateNumberStub.getDefaultImpl()!!.getPid()
            }
            _reply.readException()
            _result = _reply.readInt()
        } finally {
            _reply.recycle()
            _data.recycle()
        }
        return _result
    }

    override fun asBinder(): IBinder = mRemote

    companion object {
        var sDefaultImpl: ICalculateNumber? = null
    }
}